package com.whut.monitor.controller;

import com.whut.monitor.model.PatternEntity;
import com.whut.monitor.service.IPatternService;
import com.whut.monitor.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CFlower on 2017/11/8.
 */
@Controller
@RequestMapping("/pattern")
public class PatternController {
    @Autowired
    private IPatternService patternService;

    @RequestMapping("/manager")
    public String manager() {
        return "/mintor/sensorConfiguration/pattern";
    }

    @RequestMapping("/dataGrid")
    @ResponseBody
    public Grid dataGrid(PatternVo patte, PageFilter pageFilter) {
        Grid grid = new Grid();
        grid.setRows(patternService.dataGrid(patte, pageFilter));
        grid.setTotal(patternService.count(patte, pageFilter));
        return grid;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseMsg add(PatternVo patte) {
        ResponseMsg j = new ResponseMsg();
        try {
            patternService.add(patte);
            j.setSuccess(true);
            j.setMsg("添加成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResponseMsg edit(PatternVo patte) {
        ResponseMsg j = new ResponseMsg();
        try {
            patternService.edit(patte);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseMsg delete(Integer id) {
        ResponseMsg j = new ResponseMsg();
        try {
            patternService.delete(id);
            j.setMsg("删除成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/tree")
    @ResponseBody
    public List<Tree> tree() {
        return patternService.tree();
    }

    @RequestMapping("/getPatternByEquipment")
    @ResponseBody
    public List<ComboVo> getPatternByEquipment(Integer equipmentId) {
        List<ComboVo> list=new ArrayList<>();
        List<PatternEntity> entity=patternService.getPatternByEquipment(equipmentId);
        for (PatternEntity patternEntity : entity) {
            ComboVo comboVo=new ComboVo();
            comboVo.setValue(patternEntity.getId());
            comboVo.setText(patternEntity.getPatternName());
            comboVo.setNum(patternEntity.getPattern());
            list.add(comboVo);
        }
        return list;
    }

}
